package com.heima.xxljob.job;


import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import sun.plugin.javascript.navig.Array;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component
public class HelloJob {

    @Value("${server.port}")
    private String port;


    @XxlJob("demoJobHandler")
    public void helloJob(){
        System.err.println("简单任务执行了....");
    }

    @XxlJob("shardingJobHandler")
    public void shardingJobHandler(){
        //获取分片参数
        int shardIndex = XxlJobHelper.getShardIndex(); //当前分片
        int shardTotal = XxlJobHelper.getShardTotal(); //总分片

        //业务逻辑
        List<Integer> tasks = getList();
        for (Integer task : tasks) {
            if (task % shardTotal == shardIndex){
                System.out.println("\033[32m" + "分片" + shardIndex + "正在执行第" + task + "个任务");
            }
        }
    }

    private List<Integer> getList() {
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < 10000; i++) {
            list.add(i);
        }
        return list;
    }


}
